FUZZER_NAME := 'libfuzzer_stb_image'
PROJECT_DIR := absolute_path(".")
CARGO_TARGET_DIR := env("CARGO_TARGET_DIR", "target")
PROFILE := env("PROFILE", "release")
PROFILE_DIR := if PROFILE == "release" { "release" } else if PROFILE == "dev" { "debug" } else { "debug" }

EXTENSION := if os() == "windows" {".exe"} else { "" }
LIBAFL_CC := PROJECT_DIR / CARGO_TARGET_DIR / PROFILE_DIR / "libafl_cc" + EXTENSION
LIBAFL_CXX := PROJECT_DIR / CARGO_TARGET_DIR / PROFILE_DIR / "libafl_cxx" + EXTENSION
 
FUZZER := PROJECT_DIR / CARGO_TARGET_DIR / PROFILE_DIR / FUZZER_NAME + EXTENSION

alias cc := cxx

cxx:
    cargo build --profile {{PROFILE}}

fuzzer: cxx
    #!/bin/bash
    cargo build --profile {{PROFILE}}
    cp {{FUZZER}} .

[linux]
[macos]
run: fuzzer
    #!/bin/bash
    ./{{FUZZER_NAME}} &
    ./{{FUZZER_NAME}}
    sleep 0.2

[windows]
run: fuzzer
    echo "Not integrated into just yet."

[linux]
[macos]
test: fuzzer
    #!/bin/bash
    rm -rf libafl_unix_shmem_server || true
    (timeout 31s ./{{FUZZER_NAME}} | tee fuzz_stdout.log 2>/dev/null || true) &
    sleep 0.2
    timeout 30s ./{{FUZZER_NAME}} >/dev/null 2>/dev/null || true
    if grep -qa "corpus: 30" fuzz_stdout.log; then
        echo "Fuzzer is working"
    else
        echo "Fuzzer does not generate any testcases or any crashes"
        exit 1
    fi

clean:
    #!/bin/bash
    rm -f {{FUZZER_NAME}}
    cargo clean